package com.example.kugou.controller;

import com.example.kugou.common.RandomAccount.RandomAccount;
import com.example.kugou.common.fileUpload.FileUploadTool;
import com.example.kugou.common.wyyid.WyyIdUtil;
import com.example.kugou.pojo.Personal_data;
import com.example.kugou.service.Personal_dataService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/person")
@Api(tags = "用户相关接口",description = "提供用户相关的Rest API")
public class Personal_dataController {

    @Resource
    Personal_dataService personal_dataService;

    public Personal_dataService getPersonal_dataService() {
        return personal_dataService;
    }

    public void setPersonal_dataService(Personal_dataService personal_dataService) {
        this.personal_dataService = personal_dataService;
    }
    @ApiOperation("验证手机号是否存在")
    @PostMapping("/YanZhengPhone")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value="用户手机号",required = false),
    })
    public Map<String, Object> YanZhengPhone(String phone) {
        return personal_dataService.YanZhengPhone(phone);
    }

    @ApiOperation("查询用户信息")
    @PostMapping("/queryPersonById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid",value="用户id",required = false),
    })
    public Map<String, Object> queryPersonById(int pid) {
        return personal_dataService.queryPersonById(pid);
    }



    @ApiOperation("注册用户")
    @PostMapping("/ZhuCe")
    public Map<String, Object> ZhuCe(@ApiParam(name = "personal_data",value = "用户对象",required = true)Personal_data personal_data) {
        String account = RandomAccount.generate();
        String wyyId = WyyIdUtil.achieveCode();
        personal_data.setAccount(account);
        personal_data.setWyyid(wyyId);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> personMap =personal_dataService.ZhuCe(personal_data);
        map.put("msg", "注册失败");
        if (personMap != null) {
            map.put("msg", "注册成功");
        }
        return map;
    }

    @ApiOperation("登录")
    //    loginByAccountAndPassword
    @PostMapping("/loginByAccountAndPassword")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value="用户账号",required = false),
            @ApiImplicitParam(name = "password",value="用户密码",required = false),
    })
    public Map<String, Object> loginByAccountAndPassword(String account, String password) {
        Map<String, Object> personMap = personal_dataService.loginByAccountAndPassword(account, password);
        System.out.println(account);
        System.out.println(password);
        System.out.println(personMap);
        return personMap;
    }

    @ApiOperation("修改个人信息")
    @PostMapping(value = "/updatePerson_Data",produces = "application/json;charset=UTF-8")
    public Map<String,Object> updatePerson_Data(@ApiParam(name = "personal_data",value = "用户对象",required = true)Personal_data personal_data){
         return personal_dataService.updatePerson_Data(personal_data);
}

    /**
     * 有file文件时
     * @param inputfile 图片file
     */
    @ApiOperation("上传个人头像")
    @PostMapping(value = "/updatePersonSrc")
    @ResponseBody
    public String updatePersonSrc(MultipartFile inputfile){
        String result = "";
        try {
            result = FileUploadTool.upload("E:\\小组项目\\网易云\\网易云\\163mHome\\img\\person",inputfile,"img\\person");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }




}
